<template>
  <div id="container">
    <div class="loginContainer">
      <el-card class="registerBox" header="就爱商城注册页面">
        <el-form
          :model="formData"
          :rules="rules"
          ref="formData"
          label-width="100px"
          class="demo-formData"
        >
          <el-form-item label="用户名" prop="username">
            <el-input v-model="formData.username"></el-input>
          </el-form-item>

          <el-form-item label="密码" prop="password">
            <el-input
              type="password"
              v-model="formData.password"
              auto-complete="off"
            ></el-input>
          </el-form-item>

          <el-form-item label="确认密码" prop="repassword">
            <el-input
              type="password"
              v-model="formData.repassword"
              auto-complete="off"
            ></el-input>
          </el-form-item>

          <el-form-item class="registerBotton">
            <el-button type="primary" @click="submitForm('formData')"
              >注册</el-button
            >

            <el-button type="primary" @click="resetForm('formData')"
              >重置</el-button
            >
            <el-button type="primary" @click="gologin">返回</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </div>
</template>

<script>
import {Register} from '../../api/users'

export default {
  //判断
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.formData.checkPass !== "") {
          this.$refs.formData.validateField("checkPass");
        }

        callback();
      }
    };

    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.formData.password) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };

    return {

      formData: {
        username: "",

        password: "",

        repassword: "",
      },
      //表单验证
      rules: {
        username: [
          { required: true, message: "请输入您的名称", trigger: "blur" },

          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],

        password: [
          { required: true, validator: validatePass, trigger: "blur" },
        ],

        repassword: [
          { required: true, validator: validatePass2, trigger: "blur" },
        ],
      },
    };
  },

  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // 用户名
          var userName = this.formData.username;
          // 密码
          var password = this.formData.password;
          // 确认密码
          var repassword = this.formData.repassword;
          if (password === repassword) {
            Register(userName, password, repassword)
              .then((data) => {
                if (data.code === 1000) {
                  // 添加加载中
                  this.addConfirmLoading = false;
                  // 关闭添加模态框
                  this.addUserForm = false;
                 
                 
                  (this.formData.username = ""), (this.formData.password = "");
                  this.formData.repassword = "";
                  this.$router.replace("/login");
                } else if (data.code === 1001) {
                  // 添加加载中
                  this.addConfirmLoading = false;
                  // 用户添加失败（用户名重复）后提示信息
                  // 清空输入框
                  (this.formData.username = ""), (this.formData.password = "");
                  this.formData.repassword = "";
                  this.$message({
                    showClose: true,
                    message: "用户名重复，请重新尝试",
                    type: "warning",
                  });
                }
                console.log(data.msg);
                console.log(data);
              })
              .catch((err) => {
                console.log(err);
              });
          } else {
            this.$message({
              showClose: true,
              message: "两次密码不一致",
              type: "error",
            });
          }

          // this.activeName: 'first',
        } else {
          console.log("error submit!!");

          return false;
        }
      });
    },
    gologin() {
      //指定跳转地址
      this.$router.replace("/login");
    },

    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style scoped>
#container {
  background-size: cover;
  background-repeat: no-repeat;
  height: 100vh;
  justify-content: center;
  display: flex;
  align-items: center;
}
.registerBox {
  width: 35rem;
  min-width: 35rem;
}
.registerBotton {
  margin-left: 10rem;
}
</style>